// var qcloud = require('../vendor/wafer2-client-sdk/index')
// var config = require('../config')
// var util = require('../utils/util.js')
// var app = getApp()

function uploadFile(data, callback) {
  let promise = new Promise(function (resolve, reject) {
    // 请求用到的参数
    var Bucket = 'aa-1257564155';
    var Region = 'ap-chengdu';
    var URL = 'https://' + Bucket + '.cos.' + Region + '.myqcloud.com/';
    var FILE = data.file;
    var TYPE = data.type;
    var key = FILE.substr(FILE.lastIndexOf('/') + 1)
    // var Key = FILE.substr(FILE.lastIndexOf('/') + 1);
    wx.teamApi.account.cos({ httpMethod: 'post' }).then(res => {
      var data = res.data.result.data.credentials;
      let params = {
        httpMethod: 'post',
        httpParameters: '',
        httpHeaders: `Host: ${URL}; Content-Type: text/xml`,
        httpUri: FILE
      }
      wx.teamApi.account.cosGetauthorizationstring(params).then(signres => {
        wx.uploadFile({
            url: URL,
            name: 'file',
            filePath: FILE,
            formData: {
              'key': key,
              'success_action_status': 200,
              'Signature': signres,
              'x-cos-security-token': data.sessionToken
            },
            success: function (res) {}
        })
      })
      // callback({
      //   TmpSecretId: data.credentials && data.credentials.tmpSecretId,
      //   TmpSecretKey: data.credentials && data.credentials.tmpSecretKey,
      //   XCosSecurityToken: data.credentials && data.credentials.sessionToken,
      //   ExpiredTime: data.expiredTime,
      // });
    })
    // wx.request({
    //   url: 'http://118.24.131.46:9002/account/cos/getauthorizationstring',
    //   data: {
    //     name: 'http://tmp/wx506b54bb2e22822a.o6zAJs3iBrGDy1YbSRunjO2GIcd8.hURRrEfU1TRH81e0057aead6150aeab13935eb8118fc.jpg',
    //     type: 'filename'
    //   },
    //   dataType: 'json',
    //   success: function (res) {
    //     console.log(res)
    //   }
    // })
    // qcloud.request({
    //   url: app.data.ipUrl + 'cos/auth',
    //   method: 'POST',
    //   data: {
    //     method: 'POST',
    //     name: FILE,
    //     type: TYPE
    //   },
    //   header: {
    //     "Content-Type": "application/x-www-form-urlencoded"
    //   },
    //   dataType: 'json',
    //   success: function (result) {
    //     console.log(result)
    //     var AuthData = result.data
    //     wx.uploadFile({
    //       url: URL,
    //       name: 'file',
    //       filePath: FILE,
    //       formData: {
    //         'key': AuthData.key,
    //         'success_action_status': 200,
    //         'Signature': AuthData.Authorization,
    //         'x-cos-security-token': AuthData.XCosSecurityToken,
    //         'Content-Type': '',
    //       },
    //       success: function (res) {
    //         if (res.statusCode === 200) {
    //           util.showSuccess('上传成功')
    //           resolve(AuthData.key);
    //           app.data.resImgUrl = AuthData.key

    //         } else {
    //           reject(new Error())
    //           util.showModel('上传失败', res)
    //         }
    //       },
    //       fail: function (res) {
    //         util.showModel('上传失败', res)
    //       }
    //     });
    //     wx.hideToast()
    //   },
    //   fail: function (err) {
    //     wx.hideToast()
    //   }
    // });
  });
  return promise;
}
module.exports = {
  uploadFile: uploadFile
}; 